home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4344 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  100 lines

  1. Path: lerc.nasa.gov!purdue!yuma!usenet
  2. From: shuyuan xuan <xuan>
  3. Newsgroups: comp.lang.c
  4. Subject: why doesn't atof work properly ?
  5. Date: 3 Feb 1996 21:10:09 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4f0ivh$3g7e@yuma.ACNS.ColoState.EDU>
  8. NNTP-Posting-Host: glazunov.cs.colostate.edu
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------805478414378"
  12. X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.01 9000/715)
  13. X-URL: file:/s/chopin/c/grad/xuan/c5/m.c
  14.  
  15. This is a multi-part message in MIME format.
  16.  
  17. ---------------------------------805478414378
  18. Content-Transfer-Encoding: 7bit
  19. Content-Type: text/plain; charset=us-ascii
  20.  
  21. Thanx in advance.
  22. I was reading from da.dat, making everything into a char string. 
  23. Then I used strtok to interpret the string, converting them into int or 
  24. double variables through atoi or atof. But atof doesn't seem to work
  25. at all. Enclosed is the file, da.dat and output.
  26.  
  27. Help urgently needed. I've been thinking for days.
  28.  
  29. xuan
  30.  
  31. ---------------------------------805478414378
  32. Content-Transfer-Encoding: 7bit
  33. Content-Type: text/plain
  34.  
  35. #include "stdio.h"
  36. #include "strings.h"
  37. #include "string.h"
  38.  
  39. main()
  40. {
  41.  FILE * infile;
  42.  char contents[600],line[30];
  43.  char * s, * buf;
  44.  int i,j,l,n;
  45.  double num;
  46.  
  47.  infile=fopen ("da.dat","r");
  48.  i= 0;
  49.  while((fgets(line,29,infile))!=NULL && i<30)
  50.  {
  51.   strcat(contents,line);
  52.   i++;
  53.  }
  54.  
  55.  fclose(infile);
  56.  j= atoi(strtok(contents," \n\t\r"));
  57.  for (n=0; n<j; n++)
  58.  {
  59.    l= atoi(strtok(NULL," \n\t\r"));
  60.    for (i=0;i<l;i++)
  61.    {
  62.      s=strtok(NULL," \n\t\r");
  63.      num= atof(s);
  64.      printf(" %lf",num);
  65.    }
  66.    printf("\n");
  67.  }
  68. }
  69.  
  70. /**** the da.dat file ******
  71. 4
  72. 4
  73. 0 1.1 2.2 3.3
  74.  
  75. 3
  76. 1.0 2.0 .3
  77.  
  78. 2
  79. 1.0 2.0
  80.  
  81. 5
  82. 0 1.0 .2 .3 4.0
  83.  
  84. ******the output **********
  85.  
  86. Script started on Sat Feb  3 13:59:50 1996
  87. glazunov> m
  88.  
  89.  0.000000 1072798105.000000 1073846681.000000 1074423398.000000
  90.  1072693248.000000 1073741824.000000 1070805811.000000
  91.  1072693248.000000 1073741824.000000
  92.  0.000000 1072693248.000000 1070176665.000000 1070805811.000000 1074790400.000000
  93. glazunov> exit
  94.  
  95. glazunov> 
  96. script done on Sat Feb  3 13:59:55 1996
  97. *****************************/
  98.  
  99. ---------------------------------805478414378--
  100.